/*----------------------------------------------
| noka accordion v1.0 www.97521.com            |
| rebin 2016-07-23                             |
|---------------------------------------------*/
var nokaaccordion = Class.create({
	version: '1.0',
	initialize : function(cfg) {
	   this.id = cfg.id;
	   this.menu=cfg.menu || [] ;//菜单
	   this.open = cfg.open || 'all';//初始展开的菜单序号all 所有  0,1,2展开的序号
	   this.cent = cfg.cent;//更新内容的div
	   this.isMin = false;//是否迷你模式
	   this.sty= cfg.sty || 1;//展开模式 1 同时只能展开一级   2同时要吧展开多级
	   this.nowOpen = undefined;//当前展开的菜单
	   this.nowcliek = undefined;//当前选种的菜单
	   this.minwidth = cfg.minwidth || 40;//最小宽度
	   this.maxwidth = cfg.maxwidth || 180;//最大宽度
	   this.sidebaLeft = cfg.sidebaLeft || 8;
	   this.rnowcliek = undefined;
	   this.cookiemid = {m1:'',m2:'',m3:''};//菜单ID记录
	   this.keepstate= (undefined == cfg.keepstate?false:cfg.keepstate);//保持状态
	   this.menulist=[];
	   this.modechange = cfg.modechange;
	},
	show : function(){
		this.userEvent();
	    this.createMenu();
	    this.initCookiemenu();
	},
	//------------------------------------------------
	userEvent : function(){
		var self = this;
		$(self.id).reload = function(menu){
			self.menu = menu || [];
			self.isMin = false;
			self.maxShow();
			self.createMenu();
			self.initCookiemenu();
		};
	},
	//------------创建菜单-------------------------------
	createMenu : function(){//[{muid:'',name:'',url:'',img:'',target:'',onopen:'',onclose:'',onclick:'',onimgclick:'',sub:[]}]//关闭时调用的方法,展开时调用的方法,点击时调用的方法,点击图标时调用的方法
		var idx = 0;
        var html = [];
        var self = this;
        html[idx++]=self.createTag();
        html[idx++]='<a id="'+self.id+'_menu_alink" href="" target="" style="display:none;"></a>';
        html[idx++]='<div  class="noka_tag_scope_sidebar" id="'+self.id+'_scope_sidebar">|||</div>';//收缩按钮
        for(var i=0;i<self.menu.length;i++){
        	var menu = self.menu[i];
        	var sub = menu.sub;
        	var muid = menu.muid;
        	//------------------一级菜单-----------------------------------------------------
        	html[idx++]='<div class="noka_tag_sidebar_title" id="'+self.id+'_sidebar_'+muid+'">';
        	html[idx++]='<span class="noka_tag_sidebar_span_left_open" id="'+self.id+'_span_left_'+muid+'">&nbsp;</span>';
        	html[idx++]='<span class="noka_tag_sidebar_span_midel" id="'+self.id+'_span_midel_'+muid+'">'+menu.name+'</span>';
        	html[idx++]='<span class="noka_tag_sidebar_span_right" id="'+self.id+'_span_right_'+muid+'" style="'+(undefined == menu.img?'':'background:url('+menu.img+') no-repeat;')+'">&nbsp;</span>';
        	html[idx++]='</div>';
        	self.menulist[self.id+'_sidebar_'+muid]=menu;//一级菜单
        	//-----------------二级菜单------------------------------------------------------
        	html[idx++]='<div class="noka_tag_menu_sidebar_body" id="'+self.id+'_body_'+muid+'">';
        	if(undefined!=sub){
        		for(var a=0;a<sub.length;a++){
                	var senu = sub[a];
                	var smuid=senu.muid;
                	html[idx++]='<div class="noka_tag_menu_sidebar_button" id="'+self.id+'_button_'+smuid+'">';
                	html[idx++]='<span class="noka_tag_sidebar_button_left" id="'+self.id+'_button_left_'+smuid+'" style="'+(undefined == senu.img?'':'background:url('+senu.img+') no-repeat;')+'">&nbsp;</span>';
                	html[idx++]='<span class="noka_tag_sidebar_button_midel" id="'+self.id+'_button_midel_'+smuid+'">'+senu.name+'</span>';
                	html[idx++]='<span style="display: none;" id="'+self.id+'_menu_json_'+smuid+'">'+Object.toJSON(senu)+'</span>';
                	html[idx++]='</div>';
                	self.menulist[self.id+'_button_'+smuid]=senu;//二级菜单
        		}
        	}
        	html[idx++]='</div>';
        }
        var menubody = $(self.id);
        menubody.addClassName('noka_tag_menu_accordion');
        menubody.innerHTML= html.join('');
        var rightbody = $(self.id+'_rightsub');
        rightbody.innerHTML= '';
	    rightbody.addClassName('noka_tag_menu_raccordion');
	    rightbody.hide();
        self.resetHeight();
        self.initOpen();//初始化展开的菜单
        self.sidebarEvent();//加载点击事件
        self.resizeBody();
	},
	//------------------创建子菜单-------------------------------------------------------------
	createSubMenu : function(meun){
		var idx = 0;
        var html = [];
		var self = this;
		html[idx++]='<div  class="noka_tag_scope_rsidebar" id="'+self.id+'_scope_rsidebar">|||</div>';//收缩按钮
		if(undefined!=meun){
    		for(var a=0;a<meun.length;a++){
            	var senu = meun[a];
            	var smuid=senu.muid;
            	if(self.rnowcliek!=undefined && self.rnowcliek.id==self.id+'_rbutton_'+smuid){
            		html[idx++]='<div class="noka_tag_menu_rsidebar_button_select" id="'+self.id+'_rbutton_'+smuid+'">';
            	}else{
            		html[idx++]='<div class="noka_tag_menu_rsidebar_button" id="'+self.id+'_rbutton_'+smuid+'">';
            	}
            	html[idx++]='<span class="noka_tag_sidebar_button_left" id="'+self.id+'_rbutton_left_'+smuid+'" style="'+(undefined == senu.img?'':'background:url('+senu.img+') no-repeat;')+'">&nbsp;</span>';
            	html[idx++]='<span class="noka_tag_sidebar_rbutton_midel" id="'+self.id+'_rbutton_midel_'+smuid+'">'+senu.name+'</span>';
            	html[idx++]='<span style="display: none;" id="'+self.id+'_rmenu_json_'+smuid+'">'+Object.toJSON(senu)+'</span>';
            	html[idx++]='</div>';
            	self.menulist[self.id+'_rbutton_'+smuid]=senu;//三级菜单
    		}
    	}
		var menubody = $(self.id+'_rightsub');
	    menubody.addClassName('noka_tag_menu_raccordion');
	    menubody.innerHTML= html.join('');
	    self.resubMenuLeft();
	    if(self.rnowcliek!=undefined){
	    	self.rnowcliek=$(self.rnowcliek.id);
	    }
	    self.subMenuEvent();
	},
	resetHeight : function(){
		var self = this;
		var leftHeight = $(self.id+'_leftmenu_height').getHeight();
		var menubody = $(self.id);
		var mheight = (leftHeight-menubody.positionedOffset().top);
		menubody.setStyle({height:mheight+'px'});
		menubody.style.height = mheight+'px';
	},
	top:function(){
    	return document.documentElement.scrollTop||document.body.scrollTop;//$(self.id+'_div_title').scrollLeft
    },
	//----------------------------重设子菜单位置-----------------------------------
	resubMenuLeft : function(){
		var self = this;
		try{
			var supmenu = $(self.id);
			var pleft=supmenu.positionedOffset().left+supmenu.getWidth();
			var ptop =supmenu.positionedOffset().top;
			var menubody = $(self.id+'_rightsub');
			menubody.absolutize();
			menubody.setStyle({left:pleft+'px',top:ptop+'px'});
		}catch(e){}
	},
	//--------------------子菜单事件-------------------------------------------------------
	subMenuEvent : function(){
		var self = this;
		$(self.id+'_scope_rsidebar').observe('click', function() {
			$(self.id+'_rightsub').hide();
			self.resizeBody();
		});
		$$('div[id^="'+self.id+'_rbutton_"]').each(function(alink,index) {
			var al = $(alink);
	        var i=self.getnemuno(al.id);//获取菜单id
			$(alink).observe('mousemove', function() {
				$(alink).removeClassName('noka_tag_menu_rsidebar_button');
				$(alink).addClassName('noka_tag_menu_rsidebar_button_hover');
			});
			$(alink).observe('mouseout', function() {
				$(alink).removeClassName('noka_tag_menu_rsidebar_button_hover');
				$(alink).addClassName('noka_tag_menu_rsidebar_button');
			});
			$(alink).observe('click', function() {
				try{
					self.pjax($(alink).id,'');
					if(self.rnowcliek!=undefined){//清除原点击样式
						self.rnowcliek.removeClassName('noka_tag_menu_rsidebar_button_select');
						self.rnowcliek.addClassName('noka_tag_menu_rsidebar_button');
					}
					$(alink).removeClassName('noka_tag_menu_rsidebar_button');
					$(alink).addClassName('noka_tag_menu_rsidebar_button_select');
					self.rnowcliek = $(alink);
					self.minShow();
					self.resizeBody();
					if(self.keepstate){
						setCookie('m3',i);//设置二级菜单
					}
				}catch(e){}
			});
		});
	},
	//------------------初始化展开菜单-----------------------------------------------------------
	initOpen : function(){
		var self = this;
		var m1 = getCookie('m1');
		if(undefined!=m1){
			self.open = m1;
		}
		if(self.open == 'all' && self.sty==2){//如果是全部展开，不作处理,展开模式为2 同时展开多级
			return;
		}else{
			var opens = ','+self.open+',';//0,1 => ,0,1,
			if(self.sty==1){
				if(self.open.split(',').length>1){//有多个展开菜单，只取第一个
					opens=','+self.open.split(',')[0]+',';
					self.open=self.open.split(',')[0];
				}
			}
			self.nowOpen = $(self.id+'_sidebar_'+self.open);//当前展开的菜单
			$$('div[id^="'+self.id+'_sidebar_"]').each(function(alink,index) {
				 var al = $(alink);
		         var i=self.getnemuno(al.id);//获取菜单序号
		         var abody = $(self.id+'_body_'+i);
		         var spanLeft=$(self.id+'_span_left_'+i);
		         if(opens.indexOf(','+i+',')==-1){//关闭
		        	abody.hide();
		            spanLeft.removeClassName('noka_tag_sidebar_span_left_open');
		            spanLeft.addClassName('noka_tag_sidebar_span_left');
		         }
			});
		}
	},
	//------------------创建气泡--------------------------------------------------------------
	createTag : function(){
		var self = this;
		var idx = 0;
        var html = [];
        html[idx++]='<div class="noka_tag_sidebar_tag" id="'+self.id+'_tag" style="display:none;z-index:9999;">';
        html[idx++]='<div class="noka_tag_sidebar_arrow"><em></em></div>';
        html[idx++]='<div id="'+self.id+'_tag_body"></div>';
    	html[idx++]='</div>';
    	return html.join('');
	},
	//------------------显示气泡-------------------------------------------------------------
	showTag : function(info,pleft,ptop){
		var self = this;
		var tag = $(self.id+'_tag');
		var tagBody = $(self.id+'_tag_body');
		tag.setStyle({top:ptop+'px',left:pleft+'px'});
		tagBody.innerHTML=info;
		tag.show();
	},
	//-----------------隐藏气泡--------------------------------------------------------------
	hideTag : function(){
		var self = this;
		var tag = $(self.id+'_tag');
		tag.hide();
	},
	minShow : function(){
		var self = this;
		if(!self.isMin){//开启迷你模式
		//-----------------按钮-----------------------------------------------------
		 $(self.id+'_scope_sidebar').setStyle({width:self.minwidth+'px'});
		 //-----------------二级菜单---------------------------------------------------
		 $$('div[id^="'+self.id+'_button_"]').each(function(alink,index) {
			 var al = $(alink);
		     var i=self.getnemuno(al.id);//获取菜单序号
		     var buttonMidel= $(self.id+'_button_midel_'+i);
		     buttonMidel.hide();
		     al.setStyle({width:(self.minwidth-self.sidebaLeft)+'px'});
		 });
		 //----------------一级菜单----------------------------------------------------
		 $$('div[id^="'+self.id+'_sidebar_"]').each(function(alink,index) {
			 var al = $(alink);
	         var i=self.getnemuno(al.id);//获取菜单序号
	         var abody = $(self.id+'_body_'+i);
	         var spanMidel=$(self.id+'_span_midel_'+i);
	         var spanRight=$(self.id+'_span_right_'+i);
	         spanMidel.hide();
	         spanRight.hide();
	         al.setStyle({width:self.minwidth+'px'});
	         abody.setStyle({width:self.minwidth+'px'});
		 });
		 //----------------主框架-----------------------------------------------------
		 $(self.id).setStyle({width:self.minwidth+'px'});
		 self.isMin = true;//开启迷你模式
		 self.resubMenuLeft();
		 if(undefined!=self.modechange){
			 try{self.modechange(false);}catch(e){}
		 }
		}
	},
	//-------------------------开启全屏模式--------------------------------------------
	maxShow : function(){
		var self = this;
		//-----------------按钮-----------------------------------------------------
		 $(self.id+'_scope_sidebar').setStyle({width:self.maxwidth+'px'});
		 //-----------------二级菜单---------------------------------------------------
		 $$('div[id^="'+self.id+'_button_"]').each(function(alink,index) {
			 var al = $(alink);
		     var i=self.getnemuno(al.id);//获取菜单序号
		     var buttonMidel= $(self.id+'_button_midel_'+i);
		     buttonMidel.show();
		     al.setStyle({width:(self.maxwidth-self.sidebaLeft)+'px'});
		 });
		 //----------------一级菜单----------------------------------------------------
		 $$('div[id^="'+self.id+'_sidebar_"]').each(function(alink,index) {
			 var al = $(alink);
	         var i=self.getnemuno(al.id);//获取菜单序号
	         var abody = $(self.id+'_body_'+i);
	         var spanMidel=$(self.id+'_span_midel_'+i);
	         var spanRight=$(self.id+'_span_right_'+i);
	         spanMidel.show();
	         spanRight.show();
	         al.setStyle({width:self.maxwidth+'px'});
	         abody.setStyle({width:self.maxwidth+'px'});
		 });
		 //----------------主框架-----------------------------------------------------
		 $(self.id).setStyle({width:self.maxwidth+'px'});
		 self.isMin = false;//关闭迷你模式
		 self.resubMenuLeft();
		 if(undefined!=self.modechange){
			 try{self.modechange(true);}catch(e){}
		 }
	},
	//------------------加载sidebar事件-------------------------------------------------------
	sidebarEvent : function(){
		 var self = this;
		 //----------------菜单收缩按钮单击事件---------------------------------------------------
		 $(self.id+'_scope_sidebar').observe('click', function() {
			 if(!self.isMin){
				 self.minShow();
			 }else{
				self.maxShow();
			 }
			 self.resizeBody();
		 });
		 $$('div[id^="'+self.id+'_sidebar_"]').each(function(alink,index) {
			 //---------加载单击事件-----------------------------------------------------------
	         $(alink).observe('click', function() {
	            var al = $(alink);
	            var i=self.getnemuno(al.id);//获取菜单id
	            var abody = $(self.id+'_body_'+i);
	            if(abody.visible()){//可见，则隐藏
	            	self.hideMenu(al);
	            }else{
	            	self.showMenu(al);
	            	if(self.sty==1){
	            		try{
		            		if(self.nowOpen!=undefined && self.nowOpen.id!=al.id){//是同一个对象不作处理
		            			self.hideMenu(self.nowOpen);
		            		}
	            		}catch(e){}
	            		self.nowOpen = al;
	            	}
	            }
	            self.resizeBody();
	         });
	         //---------鼠标事件--------------------------------------------------------------
	         $(alink).observe('mousemove', function() {
	        	 var al = $(alink);
	        	 if(self.isMin){
	 	             var i=self.getnemuno(al.id);//获取菜单id
	        		 var info = $(self.id+'_span_midel_'+i).innerHTML;
	        		 var pleft=al.positionedOffset().left+al.getWidth()+10;
	        		 var ptop =al.positionedOffset().top;
	        		 self.showTag(info, pleft, ptop);
	        	 }
	        	al.removeClassName('noka_tag_sidebar_title');
				al.addClassName('noka_tag_sidebar_title_hover');
	         });
	         $(alink).observe('mouseout', function() {
	        	 var al = $(alink);
	        	 if(self.isMin){
	        		 self.hideTag();//隐藏气泡
	        	 }
	        	al.removeClassName('noka_tag_sidebar_title_hover');
				al.addClassName('noka_tag_sidebar_title');
	         });
	    });
		//--------------二级菜单--------------------------------------------------------------
		$$('div[id^="'+self.id+'_button_"]').each(function(alink,index) {
			var al = $(alink);
	        var i=self.getnemuno(al.id);//获取菜单id
			$(alink).observe('mousemove', function() {
				if(self.isMin){
					 var info = $(self.id+'_button_midel_'+i).innerHTML;
	        		 var pleft=al.positionedOffset().left+al.getWidth()+10;
	        		 var ptop =al.positionedOffset().top;
	        		 self.showTag(info, pleft, ptop);
				}
				$(alink).removeClassName('noka_tag_menu_sidebar_button');
				$(alink).addClassName('noka_tag_menu_sidebar_button_hover');
			});
			$(alink).observe('mouseout', function() {
				if(self.isMin){
					self.hideTag();//隐藏气泡
				}
				$(alink).removeClassName('noka_tag_menu_sidebar_button_hover');
				$(alink).addClassName('noka_tag_menu_sidebar_button');
			});
			$(alink).observe('click', function() {
				try{
					var menu = self.menulist[$(alink).id];
					if(menu.sub!=undefined){//有子菜单
						$(self.id+'_rightsub').show();
						self.createSubMenu(menu.sub);
					}else{//没有子菜单
						self.pjax($(alink).id,'');
						$(self.id+'_rightsub').hide();
					}
					if(self.nowcliek!=undefined){//清除原点击样式
						self.nowcliek.removeClassName('noka_tag_menu_sidebar_button_select');
						self.nowcliek.addClassName('noka_tag_menu_sidebar_button');
					}
					$(alink).removeClassName('noka_tag_menu_sidebar_button');
					$(alink).addClassName('noka_tag_menu_sidebar_button_select');
					self.nowcliek = $(alink);
					if(self.keepstate){
						setCookie('m2',i);//设置二级菜单
					}
				}catch(e){}
				self.resizeBody();
			});
		});
		Event.observe(window, 'resize',function(){
			setTimeout(function(){
				try{
					$(self.cent).resize(100,100);
					self.resetHeight();
					self.resizeBody();
				}catch(a){}
			}, 200);
		});
	},
	 //--------------显示菜单---------------------------------
	showMenu : function(al){
		var self = this;
        var i=self.getnemuno(al.id);//获取菜单id
        var abody = $(self.id+'_body_'+i);
        var spanLeft=$(self.id+'_span_left_'+i);
        abody.show();
        spanLeft.removeClassName('noka_tag_sidebar_span_left');
        spanLeft.addClassName('noka_tag_sidebar_span_left_open');
        if(self.keepstate){
        	setCookie('m1',i);//设置一级菜单
        }
	},
	//-----------------隐藏菜单-----------------------------
	hideMenu : function(al){
		var self = this;
        var i=self.getnemuno(al.id);//获取菜单id
        var abody = $(self.id+'_body_'+i);
        var spanLeft=$(self.id+'_span_left_'+i);
        abody.hide();
        spanLeft.removeClassName('noka_tag_sidebar_span_left_open');
        spanLeft.addClassName('noka_tag_sidebar_span_left');
	},
	resizeBody : function(){
		var self = this;
		var manLeft = $(self.id);
		var subLeft = $(self.id+'_rightsub');
		var left=manLeft.getWidth()+(subLeft.visible()?subLeft.getWidth():0);
		var bwidth = $(self.id+'_leftmenu_width').getWidth()-left;
		var bheight = manLeft.getHeight();
		try{
			var cent = $(self.cent);
			cent.absolutize();
			cent.setStyle({left:left+'px',top:manLeft.positionedOffset().top+'px',height:bheight+'px',width:bwidth+'px'});
			cent.width=bwidth+'px';
			cent.height=bheight+'px';
			cent.style.width=bwidth+'px';
			cent.style.height=bheight+'px';
		}catch(a){}
		try{
			$(self.cent).resize(bwidth,bheight);
		}catch(a){
			setTimeout(function(){
				try{
					$(self.cent).resize(bwidth,bheight);
				}catch(e){}
			}, 100);
		}
	},
	//-----------------setcookie mid-------------------------------------------------------
	initCookiemenu : function(){//type : m1 | m1 | m3
		var self = this;
		try{
			if(self.keepstate){
				var m2 = getCookie('m2');
				var m3 = getCookie('m3');
				var mjson = $(self.id+'_menu_json_'+m2).innerHTML.toJSON();
				if(undefined!=m2){
					$(self.id+'_button_'+m2).click();
				}
				if(undefined==mjson.url){
					$(self.id+'_rbutton_'+m3).click();
				}
			}
		}catch(E){}
	},
	//-----------------pjax----------------------------------------------------------------
	pjax : function(aid,pars){
		var self = this;
		var menu = self.menulist[aid];
		if(undefined!=menu.onclick){
    		try{
    			menu.onclick(menu);
    		}catch(e){}
    	}
		var alink = $(self.id+'_menu_alink');
		alink.href=menu.url;
		if(undefined!=menu.target && menu.target!=''){
			alink.target = menu.target;
		}
		//---------------------------------------------------------------------------------
		if('nktab'==menu.target){//选项卡模式
			try{//选项卡
				var tab = $(self.cent);
				tab.insertTab({id:menu.muid,title:menu.name,url:menu.url});
			}catch(e){
				setTimeout(function(){
					try{
						var tab = $(self.cent);
						tab.insertTab({id:menu.muid,title:menu.name,url:menu.url});
					}catch(e){}
				}, 200);
			}
		}else{
			alink.click();
		}
		//---------------------------------------------------------------------------------
	},
	//-----------------获取序号---------------------------------------------------------------
	getnemuno : function(muid){
		if(undefined!=muid){
			var ms = muid.split('_');
			var len = ms.length-1;
			return ms[len];
		}
		return;
	}
});